package cn.glenn._02notice.controller;

import cn.glenn._02notice.base.response.JsonResult;
import cn.glenn._02notice.base.response.StatusCode;
import cn.glenn._02notice.mapper.NoticeMapper;
import cn.glenn._02notice.pojo.dto.NoticeAddParam;
import cn.glenn._02notice.pojo.dto.NoticeAddQuery;
import cn.glenn._02notice.pojo.dto.NoticeUpdateParam;
import cn.glenn._02notice.pojo.enity.Notice;
import cn.glenn._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.glenn._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解
 * 1.组合注解，相当于Controller+ResponseBody的组合
 * 2.再累上添加该注解后，所有的控制器方法上无需再添加ResponseBody注解
 */

//@Controller
@RestController
@RequestMapping("/v1/notice/")
@Slf4j
@Tag(name="01-资讯管理模块",description = "01-资讯管理模块")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "添加资讯")
    public JsonResult noticeAdd(NoticeAddParam noticeAddParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);//复制
        /*  notice.setTitle(noticeAddParam.getTitle());
        notice.setContent(noticeAddParam.getContent());
        notice.setType(noticeAddParam.getType());
        notice.setStatus(noticeAddParam.getStatus());*/
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice输出"+notice);
        int num = noticeMapper.insertNotice(notice);
        if (num>0) {return JsonResult.success();}
        else return JsonResult.fail();
    }

    @PostMapping("list")
    @Operation(summary = "资讯列表")
    @ApiOperationSupport(order = 2)
    public JsonResult noticeList(@RequestBody NoticeAddQuery noticeAddQuery) {
        List<NoticeListVO>  noticeListVOS = noticeMapper.selectNotice(noticeAddQuery);
        return JsonResult.success(noticeListVOS);
    }

    @GetMapping("detail")
    @Operation(summary = "资讯详情")
    @ApiOperationSupport(order = 3)
    @Parameter(name="id",description = "资讯编号",required=true)
    public JsonResult noticeDetail(Long id) {
        NoticeDetailInfoVO detail = noticeMapper.selectNoticeDetail(id);
        return JsonResult.success(detail);
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 4)
    @Parameter(name="username",description = "用户名",required=false)
    @Parameter(name="id",description = "资讯编号",required=true)
    public JsonResult deleteNoticeById(Long id,String username) {
        int num = noticeMapper.deleteNoticeById(id);
        log.info("删除成功:"+num);
        if (num>0) {return JsonResult.success();}
        else return JsonResult.fail();
    }
    @PostMapping("update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 5)
    public JsonResult updateNoticeContent(NoticeUpdateParam noticeUpdateParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int num = noticeMapper.updateNoticeContent(notice);
        if (num>0) {return JsonResult.success();}
        else return JsonResult.fail();

    }
}
